package client.comm.baoding.ui

import android.view.View
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.SubsidyAdapter
import client.comm.baoding.api.bean.LevelIncome
import client.comm.baoding.databinding.ActivitySubsidyDataBinding
import client.comm.baoding.ui.vm.SubsideDataViewModel
import client.comm.commlib.comm_ui.draw.SpaceItemBottomDecoration
import client.comm.commlib.widget.EmptyLayout

/**
 * Date: 2022/11/24 10:37
 * Description: 补贴数据
 */
class SubsidyDataActivity : BaseMiddleActivity<SubsideDataViewModel, ActivitySubsidyDataBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_subsidy_data
    }

    override fun initViewModel(): SubsideDataViewModel = createViewModel()

    val adapter: SubsidyAdapter by lazy {
        SubsidyAdapter(this, mImgLoader)
    }

    override fun initView() {
        super.initView()
        binding.event = this
        binding.recyclerView.adapter = adapter
        binding.recyclerView.addItemDecoration(SpaceItemBottomDecoration(10))
        binding.refreshLayout.setOnRefreshListener {
//            adapter.hasMoreState.value = true
            viewModel.getLevelIncome()
        }
        binding.emptyLayout.setOnClickListener {
//            adapter.hasMoreState.value = true
            viewModel.getLevelIncome()
        }

        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    binding.bean = it.data //as LevelIncome
                    val list = it.data?.level_list as MutableList<LevelIncome.Level?>
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    }
                    if (list != null && list.size > 0) {
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }


    override fun initData() {
        super.initData()
        viewModel.getLevelIncome()
        adapter.hasMoreState.value = false
    }


}